<template>
  <div class="app-page">
    <div style="margin-bottom: 10px;">
      <el-button v-permission="1" type="primary" icon="el-icon-search" @click="search">查询</el-button>
      <el-button v-permission="2" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
    </div>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      highlight-current-row
      custom-column
      @fetch="fetchData"
    />
    <addPrint ref="addPrint" />
  </div>
</template>

<script>
  import addPrint from './components/addPrint.vue'
  import { parseTime } from '@/utils'
  import { getPrinterSetting } from '@/api'

  export default {
    name: 'print-config',
    components: { addPrint },
    data() {
      return {
        keyFunMap: [],
        query: {
          keyFun: '',
          keyStatus: ''
        },
        tableData: {
          total: 0,
          column: [
            {
              type: 'index',
              align: 'center'
            },
            {
              prop: 'templateType',
              label: '模版名称',
              render: (h, { row }) => <span>{ this.nameFotmatter(row.templateType) }</span>,
              width: 100
            },
            {
              prop: 'templateContent',
              label: '模版内容'
            },
            {
              prop: 'tempStatus',
              label: '模版状态',
              render: (h, { row }) => <span>{ row.tempStatus === 1 ? '作为' : row.tempStatus === 2 ? '启用' : '' }</span>,
              width: 90
            },
            {
              prop: 'createTime',
              label: '创建时间',
              width: 160,
              render: (h, { row }) => <span>{ row.createTime && parseTime(row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
            },
            {
              label: '操作',
              align: 'center',
              fixed: 'right',
              width: 120,
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link v-permission='3' underline={false} size='mini' onClick={() => this.update(row, $index)}>
                      编辑
                    </el-link>
                    <el-link v-permission='4' underline={false} size='mini' onClick={() => this.print(row, $index)}>
                      打印PDF
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    methods: {
      async fetchData() {
        const res = await getPrinterSetting()
        this.tableData.data = res.data
      },
      nameFotmatter(val) {
        if (val === 1) {
          return '配送单'
        } else if (val === 2) {
          return '瓶批销售单'
        } else if (val === 3) {
          return '槽批销售单'
        } else if (val === 4) {
          return '押金单'
        } else if (val === 11) {
          return '调拨单'
        } else if (val === 18) {
          return '报修单'
        } else if (val === 15) {
          return '押金'
        } else if (val === 19) {
          return '回收单'
        } else if (val === 20) {
          return '装卸车单'
        } else if (val === 31) {
          return '退户申请单'
        } else if (val === 32) {
          return '退瓶单'
        } else if (val === 33) {
          return '退瓶退款单'
        } else if (val === 34) {
          return '费用办理单'
        } else if (val === 35) {
          return '气瓶提取单'
        } else if (val === 36) {
          return '瓶批销售单-新版'
        } else if (val === 37) {
          return '槽批销售单-新版'
        } else if (val === 38) {
          return '配送单-新版'
        } else if (val === 39) {
          return '订单回单'
        } if (val === 40) {
          return '充装单'
        }
        return ''
      },
      async add() {
        await this.$refs.addPrint.open()
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      isJSON(str) {
        try {
          JSON.parse(str)
        } catch (e) { return false }
        return true
      },
      print(row) {
        if (row.templateContent) {
          if (!this.isJSON(row.templateContent)) {
            this.$message.warning('操作失败,请检查模板格式是否正确')
            return
          }
          var data = JSON.parse(row.templateContent)
          var printcmd = {
            'cmd': 2,
            'title': String.format('正在对开户证明{0}进行打印', 111),
            'elements': []
          }
          printcmd = { ...printcmd, ...data }
          if (data.doc) {
            var orderDetails = [
              { goodsName: '15kg液化气瓶', specName: '15kg高规格瓶', gprince: 20, gcount: 2, accReceivable: 40 },
              { goodsName: '12kg氮气瓶', specName: '12kg高规格瓶', gprince: 18, gcount: 1, accReceivable: 18 },
              { goodsName: '50kg智能瓶', specName: '50kg高规格瓶', gprince: 40, gcount: 3, accReceivable: 120 },
              { goodsName: '5kg小氢气瓶', specName: '5kg规格', gprince: 5, gcount: 4, accReceivable: 20 }
            ]

            var source = {
              printDt: '2021-09-20',
              orderCode: '1111',
              csmCode: '123333',
              csmPhone: '133883939',
              csmName: '测试',
              sfzh: '420684200808088888',
              csmAddress: '湖北省武汉市人大道100号',
              details: orderDetails,
              csmType: '居民用户',
              type: '月结',
              trukCode: '鄂A00001'
            }
            let printcmd = {
              cmd: 2,
              title: `正在对订单${row.orderCode}进行配送单打印`,
              elements: []
            }
            printcmd = { ...printcmd, ...source }
            window.jCPrint.process(printcmd, source, row, orderDetails)
            delete printcmd.els
            if (window.jCPrint.firePush(JSON.stringify(printcmd))) {
              this.$message.success('打印指令已发送')
            } else {
              this.$message.error('打印未连接')
            }
          } else {
            var itmes = [
              { goodsName: '15kg液化气瓶', specName: '15kg高规格瓶', gprince: 20, gcount: 2, accReceivable: 40 },
              { goodsName: '12kg氮气瓶', specName: '12kg高规格瓶', gprince: 18, gcount: 1, accReceivable: 18 },
              { goodsName: '50kg智能瓶', specName: '50kg高规格瓶', gprince: 40, gcount: 3, accReceivable: 120 },
              { goodsName: '5kg小氢气瓶', specName: '5kg规格', gprince: 5, gcount: 4, accReceivable: 20 }
            ]

            var rows = {
              printDt: '2021-09-20',
              orderCode: '1111',
              csmCode: '123333',
              csmPhone: '133883939',
              csmName: '测试',
              sfzh: '420684200808088888',
              csmAddress: '湖北省武汉市人大道100号',
              csmType: '居民用户',
              type: '月结',
              trukCode: '鄂A00001'
            }
            window.jCPrint.process(printcmd, data, rows, itmes)
            delete printcmd.els
            if (window.jCPrint.send(JSON.stringify(printcmd))) {
              this.$message.success('打印指令已发送')
            } else {
              this.$message.error('打印未连接')
            }
          }
        } else {
          this.$message.warning('模板内容不存在')
          return
        }
      },
      async update(row) {
        const routeData = this.$router.resolve({
          path: '/editPrint',
          query: {
            templateType: row.templateType
          }
        })
        window.open(routeData.href, 'DetailRunTime')
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      }
    }
  }
</script>

<style scoped>

</style>
